#!/usr/bin/perl

# ---------- Init variables -------
#my $version = "0.0001";
# ---------- external readable parameters -------
## Functions
##-------------------------------------------------------
#
# stict syntax checking.
require Exporter;
use strict;
use SNMP_util;
use SIM_settings;
use SIM_cgiSettings;
use SIM_base;
use SIM_cgi;
use SIM_nodeInfo;
use SIM_report;
use CGI qw(param);

#my $httpUser = $ENV{'REMOTE_USER'} || die "Oops no env found for SREMOTE_USER.";

my $treeColor = "#FFFFCC";

my $tool = $ARGV[0];
my $action = $ARGV[1];
my $location = $ARGV[2];
my $option1 = $ARGV[3];
my $option2 = $ARGV[4];
my $option3 = $ARGV[5];
my $option4 = $ARGV[6];

if ( $location eq "none" ) {
  $location = "";
}

my $baseUrl = "$httpScriptName?$tool";

# ----------------- subs ---------------------

sub listLocationDevices {
  my $location = "@_[0]";
  print("<TABLE BORDER=0 CELLSPACING=0 WIDTH=100%>");
  chdir "$SIMparam{'SIMlocationDir'}$location";
  my @devList = <device-*>;
  foreach my $devName (@devList) {
    my $outName = substr($devName, 7);
    my $info = catFileLine($devName);
    my ( $type, $value ) = split(' ', $info, 2);
    if ( $type eq "ipAddr" ) {
      my $statusColor = testNodeStatus( $value );
      print("<TR><TD BGCOLOR=$statusColor><A HREF=$httpScriptName?mon+nodestatus+$value>$outName</A></TD></TR>");
    } else {
      print("<TR><TD>$outName</TD></TR>");
    }
  }
  print("</TABLE>");
}

sub listLocationTree {
  my $location = "@_[0]";
  my $option = "@_[1]";
  my @locTree = split('\/', $location);
  chdir "$SIMparam{'SIMlocationDir'}$location";
  my @locations = <location-*>;
  foreach my $locEntry (@locations) {
    my $locName = catFileLine("$SIMparam{'SIMlocationDir'}$location/$locEntry/locationName");
    print("<TR BGCOLOR=$treeColor>");
    my $baseNum = 1;
    while( $baseNum <= $#locTree ) {
      print("<TD></TD>");
      $baseNum++;
    }
    my $locString = "$location/$locEntry";
    $locString =~ s#\/location-#\/#g;
    print("<TD VALIGN=top><A HREF=$httpScriptName?$tool+listLocation+$locString>$locName</A></TD><TD COLSPAN=99>");
    &listLocationDevices("$location/$locEntry");
    print("</TD></TR>");
#   print("<TR><TD COLSPAN=99>$#locTree $SIMparam{'SIMlocationDir'}$location/$locEntry/locationName</TD></TR>");
    if ( $option eq "tree" ) {
      listLocationTree( "$location/$locEntry", "tree");
    }
  }
}

sub getLocationTree {
  my $location = "@_[0]";
  my @locArr = split('/', $location);
  my $baseLoc = "";
  foreach my $entry (@locArr) {
    if ( $entry ne "" ) {
      $baseLoc = "$baseLoc/$entry";
      my $locName = catFileLine("$SIMparam{'SIMlocationDir'}$baseLoc/locationName");
      if ( $locName ne "" ) {
        my $locString = "$baseLoc";
        $locString =~ s#\/location-#\/#g;
        print("<TD><A HREF=$httpScriptName?$tool+listLocation+$locString>$locName</A></TD>");
      }
    }
  }
}

sub updateLocation {
  my $location = "@_[0]";
  my $workDir = "$SIMparam{'SIMlocationDir'}$location";
  printDebug(1, "addLocation Location: $workDir.");
  if ( ! -d $workDir ) {
    print("<H2>location not defined.");
  } else {
    my $locName = catFileLine("$workDir/locationName");
    if ( $location eq "" ) {
      $locName = "root";
    }
    
  }  
}

sub addLocation {
  my $location = "@_[0]";
# $location =~ s#\/#\/location-#g;
  my $workDir = "$SIMparam{'SIMlocationDir'}$location";
  printDebug(1, "addLocation Location: $workDir.");
  if ( ! -d $workDir ) {
    print("<H2>location not defined.");
  } else {
    my $locName = catFileLine("$workDir/locationName");
    if ( $location eq "" ) {
      $locName = "root";
    }
    printDebug(1, "addLocation dir found.");
    my $newLoc = param("newLocation");
    my $testName = $newLoc;
    $testName =~ tr#a-zA-Z0-9#A#cds;
    if ( $testName eq "" ) {
      $location =~ s#\/location-#\/#g;
      print("
        <FORM method=post Action=$httpScriptName?$tool+addLocation+$location>
        Add an sub location for $locName:
        <INPUT NAME=newLocation Size=20 Type=text>
        <INPUT TYPE=submit Value=\"Add location\">
        </FORM>
            ");
    } else {
      print("<H2>Adding sub location $newLoc for $locName.</H2>");
      my $lcLocation = lc($testName);
      $location = "$workDir/location-$lcLocation";
      if ( -d $location ) {
        print("<H2>location $newLoc ($lcLocation) already defined.</H2>");
      } else {
        system(" mkdir $workDir/location-$lcLocation 2>&1 ");
        printFileLine("$workDir/location-$lcLocation/locationName", "locationName", $newLoc);
        print("<P>");
      }
    }
  }
}

sub printMainMenu {
  my $type = "@_[0]";
  if ( $type eq "" ) {
    $type = "table";
  }
  if ( $type eq "table" ) {
    print("<BR><B><FONT SIZE=+2>Menu</FONT></B> (under devellopment)<TABLE>");
  }
  printSeparator("Location","$type", "#888888");
  printEntry2("root","show the location root", "$tool+$action", "$tool+listLocation", "", "$type", "", "");
  printEntry2("tree","list the location tree", "$tool+$action", "$tool+listLocationTree", "", "$type", "|", "");
  printEntry2("details","Display the site details, like documentation, contacts", "$tool+$action", "$tool+addLocation", "$location", "$type", "|", "");
  printEntry2("add","add a location in the tree (location sensitive, first select your base location via root)", "$tool+$action", "$tool+addLocation", "$location", "$type", "|", "");
  printEntry2("update","update location definitions in the tree (location sensitive, first select your base location via root)", "$tool+$action", "$tool+updateLocation", "$location", "$type", "|", "");
  printSeparator("Services","$type", "#888888");
  if ( $type eq "table" ) { print("<TR><TD></TD><TD>to be defined</TD></TR>"); }
  printSeparator("Contacts","$type", "#888888");
  if ( $type eq "table" ) { print("<TR><TD></TD><TD>to be defined</TD></TR>"); }
  if ( $type eq "table" ) {
    print("</TABLE><P>");
  }
}

# ----------------- main ---------------------
# menu stuff
&printMainMenu("line");
print("</TR></TABLE><CENTER>");

#printDebug(0, "main: location = $location");
my $dispLocation;
if ( $location ne "" ) {
  $dispLocation = $location;
  $location =~ s#\/#\/location-#g;
}

if ( ! -d $SIMparam{'SIMlocationDir'} ) {
  print("<H2>Oops, no base location info found.</H2>");
} elsif ( ! -d "$SIMparam{'SIMlocationDir'}/$location" ) {
  print("<H2>Oops, location $location not found</H2>.");
} elsif ( $action eq "addLocation" ) {
  &addLocation($location);
} elsif ( $action eq "updateLocation" ) {
  &updateLocation($location);
} elsif ( $action eq "listLocationTree" ) {
  print("<H2>Location tree</H2><TABLE>");
  listLocationTree( "", "tree");
  print("</TABLE>");
} elsif ( $action eq "listLocation" ) {
  print("<H2>Location selection</H2><TABLE>");
  print("<TABLE><TR BGCOLOR=$treeColor>");
  &getLocationTree($location);
  print("</TR>");
  listLocationTree($location);;
  print("</TABLE>");
} else {
  &printMainMenu("table");
}
